package com.example.wuguitong.timezone;

import android.content.Context;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextClock;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {
    private Button btnTimeZone;
    private Context mContext;
    private TextClock mTextClock;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnTimeZone = (Button)findViewById(R.id.timezone);
        mTextClock = (TextClock)findViewById(R.id.textClock);
        mTextClock.setTimeZone(getCurrentTimeZone());
        mContext = this;
        btnTimeZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat msdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String sTime = "1986-09-13 23:59:00";
                sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
                TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
                msdf.setTimeZone(TimeZone.getTimeZone(getCurrentTimeZone()));
                Date time = null;
                try {
                    time = sdf.parse(sTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Calendar cd = Calendar.getInstance();

                cd.setTime(time);
                TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
                //mTextClock.setTimeZone("Asia/Shanghai");
                try {
                    Toast.makeText(mContext,"TimeZone tz.useDaylightTime = " + tz.useDaylightTime() +" tz.inDaylightTime(time)"
                            + " " + tz.inDaylightTime(time) +" "+ time.toString() + " " + msdf.parse(sTime).toString()  + getCurrentTimeZone(),Toast.LENGTH_LONG).show();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                tz = TimeZone.getTimeZone(getCurrentTimeZone());
                try {
                    Toast.makeText(mContext,"TimeZone tz.useDaylightTime = " + tz.useDaylightTime() +" tz.inDaylightTime(time)"
                            + " " + tz.inDaylightTime(time) +" "+ time.toString() + " " + msdf.parse(sTime).toString(),Toast.LENGTH_LONG).show();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                // 2小时以后是几点？

                cd.add(Calendar.HOUR, 2);

                time = cd.getTime();
                try {
                    Toast.makeText(mContext,"TimeZone tz.useDaylightTime = " + tz.useDaylightTime() +" tz.inDaylightTime(time)"
                            + " " + tz.inDaylightTime(time) +" "+ time.toString() + " " + msdf.parse(sTime).toString(),Toast.LENGTH_LONG).show();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    public static String getCurrentTimeZone()
    {
        TimeZone tz = TimeZone.getDefault();
        int offsetMinutes = tz.getRawOffset()/60000;
        char sign = '+';
        if(offsetMinutes < 0){
            sign = '-';
            offsetMinutes = -offsetMinutes;
        }
        StringBuilder builder = new StringBuilder(9);
        builder.append("GMT");
        builder.append(sign);
        appendNumber(builder, 2, offsetMinutes / 60);
        builder.append(':');
        appendNumber(builder, 2, offsetMinutes % 60);
        return builder.toString();
    }
    private static void appendNumber(StringBuilder builder, int count, int value) {
        String string = Integer.toString(value);
        for (int i = 0; i < count - string.length(); i++) {
            builder.append('0');
        }
        builder.append(string);
    }
}
